﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Core;
using BaseLinQ.DataContex;
using Module;
using Solution.AppCode;

namespace _12CSolution.Cpanel
{
    public partial class ChonVaiTro : System.Web.UI.Page
    {
        string _conn = ConfigurationSettings.AppSettings["ConnectString"];
        Utils util = new Utils();
        RBACDataContext db = null;
        string tenDonVi = "";
        string iDonViID = "";
        bool check = true;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                Session[Constants.session_vaitroquanly_id] = null;
                Session[Constants.session_donviquanly_id] = null;
                tenDonVi = "";
                iDonViID = "";
                //HttpCookie cookie = HttpContext.Current.Request.Cookies["SHRM_INFOR"];
                //cookie = null;
                if (Session[Constants.session_user_id] == null)
                {
                    Response.Redirect(ResolveUrl("~/") + "Admin/Login.aspx");
                }
                db = new RBACDataContext(_conn);
                tbl_NguoiDung objUser = (from m in db.tbl_NguoiDungs
                                         where m.pk_iNguoiDungID == Utils.GetUserId()
                                         select m).FirstOrDefault<tbl_NguoiDung>();
                //lblChao.Text = "Xin chào, <span class='red'>" + objUser.sHoTen + "</span>. Xin chọn Đơn vị và Vai trò quản lý!";
                //TenUser.Text = " " + objUser.sTenDangNhap;
                db = null;
                //frmMain.Show();
                LoadCboDonVi();
            }
        }
        public void LoadCboDonVi()
        {
            db = new RBACDataContext(_conn);
            long iNguoiDungID = Utils.GetUserId();
            //long DONVI_ROOT = Utils.GetRootDepartmentID(DONVI_ID);
            DonViLogic dvLogic = new DonViLogic();
            List<tbl_Donvi> lstDonVi = dvLogic.GetOrgDepartments(iNguoiDungID);
            if (lstDonVi != null && lstDonVi.Count == 1)
            {
                //StoreDonVi.DataSource = lstDonVi;
                //StoreDonVi.DataBind();
                //cboDonVi.SelectedIndex = 0;
                tenDonVi = lstDonVi[0].sTenDonVi;
                iDonViID = lstDonVi[0].pk_iDonViID + "";
                LoadCboVaiTro();
            }
            if (lstDonVi != null && lstDonVi.Count > 0)
            {
                //StoreDonVi.DataSource = lstDonVi;
                //StoreDonVi.DataBind();
                lstDonVi = null;
            }
            dvLogic = null;
        }
        public void LoadCboVaiTro()
        {
            db = new RBACDataContext(_conn);
            tbl_Donvi objDonvi = (from m in db.tbl_Donvis
                                  where m.pk_iDonViID == long.Parse(iDonViID)
                                  && m.Status == true
                                  select m).FirstOrDefault<tbl_Donvi>();
            long DONVI_ID = objDonvi.pk_iDonViID;
            long UserID = Utils.GetUserId();
            var objVTNDDV = from n in db.tbl_VaiTroNguoiDungDonVis
                            where n.fk_iNguoiDungID == UserID
                            && n.fk_iDonViID == DONVI_ID
                            && n.bKichHoat == true
                            select n;
            List<tbl_VaiTro> lstVaiTro = new List<tbl_VaiTro>();
            long chk = 0;
            foreach (tbl_VaiTroNguoiDungDonVi VTNDDV in objVTNDDV)
            {
               tbl_VaiTro objVT = (from j in db.tbl_VaiTros
                                    where j.pk_iVaiTroID == VTNDDV.fk_iVaiTroID
                                    && j.bKichHoat == true
                                    select j).FirstOrDefault<tbl_VaiTro>();
                if (objVT != null && objVT.pk_iVaiTroID != chk)
                {
                    lstVaiTro.Add(objVT);
                    chk = objVT.pk_iVaiTroID;
                }
            }
            if (lstVaiTro.Count == 1 && iDonViID != "" && check == true)
            {
                //StoreVaiTro.DataSource = lstVaiTro;
                //StoreVaiTro.DataBind();
                //cboDonVi.SelectedIndex = 0;

                Session[Constants.session_vaitroquanly_id] = lstVaiTro[0].pk_iVaiTroID + "";
                Session[Constants.session_donviquanly_id] = iDonViID;
                lstVaiTro = null;
                Response.Redirect(ResolveUrl("~/Admin/Default.aspx"));

            }
            else if (lstVaiTro.Count > 0)
            {
                //StoreVaiTro.DataSource = lstVaiTro;
                //StoreVaiTro.DataBind();
                lstVaiTro = null;

            }
            else if (lstVaiTro.Count == 0)
            {
                //StoreVaiTro.RemoveAll();
            }
            //cboVaiTro.Clear();
            db = null;
        }
        public void cboDonVi_Change(object sender, EventArgs e)
        {
            check = false;
           // iDonViID = cboDonVi.Text;
            LoadCboVaiTro();
        }
        //[DirectMethod]
        public void Accept()
        {
            //if (cboDonVi.SelectedIndex == -1)
            //{
            //    Tools.message_HRM("Bạn chưa chọn Đơn vị!");
            //    return;
            //}
            //if (cboVaiTro.SelectedIndex == -1)
            //{
            //    Tools.message_HRM("Bạn chưa chọn Vai Trò");
            //    return;
            //}
            //HttpCookie cookie = HttpContext.Current.Request.Cookies["SHRM_INFOR"];
            //Session[Constants.session_vaitroquanly_id] = cboVaiTro.Text;
            //Session[Constants.session_donviquanly_id] = cboDonVi.Text;
            //Response.Cookies.Add(cookie);
            Response.Redirect(ResolveUrl("~/Admin/Default.aspx"));
        }
        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            //if (cboDonVi.SelectedIndex == -1)
            //{
            //    Tools.message_HRM("Bạn chưa chọn Đơn vị!");
            //    return;
            //}
            //if (cboVaiTro.SelectedIndex == -1)
            //{
            //    Tools.message_HRM("Bạn chưa chọn Vai Trò");
            //    return;
            //}
            //HttpCookie cookie = HttpContext.Current.Request.Cookies["SHRM_INFOR"];
            //Session[Constants.session_vaitroquanly_id] = cboVaiTro.Text;
            //Session[Constants.session_donviquanly_id] = cboDonVi.Text;
            //Response.Cookies.Add(cookie);
            Response.Redirect(ResolveUrl("~/Admin/Default.aspx"));
        }
    
    }
}